﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace OpenSearchTests
{
    [TestClass]
    public class EsaVirtArchive
    {
        [TestMethod]
        [DeploymentItem("esa_virt_archive_osdd.xml")]
        public void TestParseEsaVirtArchiveOsdd()
        {
            System.IO.FileStream stream = new System.IO.FileStream("esa_virt_archive_osdd.xml", System.IO.FileMode.Open);
            OpenSearch.Osdd.OpenSearchDescriptionDocument osdd = new OpenSearch.Osdd.OpenSearchDescriptionDocument(stream);

            Assert.AreEqual("ER02_SAR_RAW_0P", osdd.ShortName);
            Assert.AreEqual("Dataset Search ER02_SAR_RAW_0P", osdd.LongName);

            Assert.AreEqual("This Search Service performs spatial, temporal and other queries in the available products of ER02_SAR_RAW_0P. There are several URL templates that return the results in different formats (RDF, ATOM or KML). This search service is in accordance with the OGC 10-032r3 specification.", osdd.Description);

            // This tags list isn't spec compliant, but we try to be tolerant in what we receive
            List<string> expectedTags = new List<string> { "Supersite", "ESA", "NASA", "GEO", "NSF", "JAXA", "UNAVCO", "Digital Earth Communities", "Catalogue" };
            List<string> actualTags = osdd.Tags.ToList<string>();
            Assert.IsTrue(expectedTags.SequenceEqual(actualTags));

            Assert.AreEqual("info@terradue.com", osdd.Contact);
            Assert.AreEqual("Terradue GeoSpatial Development Team", osdd.Developer);
            // This isn't what Attribution means, but its what the developers put in...
            Assert.AreEqual("     Terradue Srl. Copyright 2008-11, All Rights Reserved", osdd.Attribution);
            Assert.AreEqual("open", osdd.SyndicationRight.ToString());
            Assert.IsFalse(osdd.AdultContent);

            List<OpenSearch.Query> queries = osdd.Queries.ToList();
            Assert.AreEqual(1, queries.Count);
            var q = queries[0];

            OpenSearch.Query query = queries[0];
            Assert.AreEqual("example", query.Role);
            Assert.AreEqual("-25,30,45,70", query.GetExtendedAttribute(OpenSearch.Constants.NSOpenSearchGeo, "box"));

            List<OpenSearch.Osdd.Url> urls = osdd.Urls.ToList();
            Assert.AreEqual(7, urls.Count);

            OpenSearch.Osdd.Url url0 = urls.ToArray()[0];
            Assert.AreEqual(0, url0.IndexOffset);
            Assert.AreEqual(0, url0.PageOffset);
            Assert.AreEqual("application/rdf+xml", url0.MimeType);
            Assert.AreEqual("http://eo-virtual-archive4.esa.int/search/ER02_SAR_RAW_0P/rdf/?count={count?}&name={geo:name?}&startPage={startPage?}&startIndex={startIndex?}&sort={sru:sortKeys?}&q={searchTerms?}&start={time:start?}&stop={time:end?}&bbox={geo:box?}&geometry={geo:geometry?}&uid={geo:uid?}&processingCenter={eop:processingCenter?}&acquisitionStation={eop:acquisitionStation?}&orbitDirection={eop:orbitDirection?}&orbitNumber={eop:orbitNumber?}&frame={eop:wrsLatitudeGrid?}&track={eop:wrsLongitudeGrid?}&swath={eop:swathIdentifier?}&polarisation={sar:polarisationChannels?}&submitted={dct:dateSubmitted?}", url0.Template.ToString());

            OpenSearch.Osdd.Url url1 = urls.ToArray()[1];
            Assert.AreEqual(0, url1.IndexOffset);
            Assert.AreEqual(0, url1.PageOffset);
            Assert.AreEqual("text/plain", url1.MimeType);
            Assert.AreEqual("http://eo-virtual-archive4.esa.int/search/ER02_SAR_RAW_0P/wkt/?count={count?}&name={geo:name?}&startPage={startPage?}&startIndex={startIndex?}&sort={sru:sortKeys?}&q={searchTerms?}&start={time:start?}&stop={time:end?}&bbox={geo:box?}&geometry={geo:geometry?}&uid={geo:uid?}&processingCenter={eop:processingCenter?}&acquisitionStation={eop:acquisitionStation?}&orbitDirection={eop:orbitDirection?}&orbitNumber={eop:orbitNumber?}&frame={eop:wrsLatitudeGrid?}&track={eop:wrsLongitudeGrid?}&swath={eop:swathIdentifier?}&polarisation={sar:polarisationChannels?}&submitted={dct:dateSubmitted?}", url1.Template.ToString());

            OpenSearch.Osdd.Url url2 = urls.ToArray()[2];
            Assert.AreEqual(0, url2.IndexOffset);
            Assert.AreEqual(0, url2.PageOffset);
            Assert.AreEqual("text/plain", url2.MimeType);
            Assert.AreEqual("http://eo-virtual-archive4.esa.int/search/ER02_SAR_RAW_0P/txt/?count={count?}&name={geo:name?}&startPage={startPage?}&startIndex={startIndex?}&sort={sru:sortKeys?}&q={searchTerms?}&start={time:start?}&stop={time:end?}&bbox={geo:box?}&geometry={geo:geometry?}&uid={geo:uid?}&processingCenter={eop:processingCenter?}&acquisitionStation={eop:acquisitionStation?}&orbitDirection={eop:orbitDirection?}&orbitNumber={eop:orbitNumber?}&frame={eop:wrsLatitudeGrid?}&track={eop:wrsLongitudeGrid?}&swath={eop:swathIdentifier?}&polarisation={sar:polarisationChannels?}&submitted={dct:dateSubmitted?}", url2.Template.ToString());

            OpenSearch.Osdd.Url url3 = urls.ToArray()[3];
            Assert.AreEqual(0, url3.IndexOffset);
            Assert.AreEqual(0, url3.PageOffset);
            Assert.AreEqual("text/tab-separated-values", url3.MimeType);
            Assert.AreEqual("http://eo-virtual-archive4.esa.int/search/ER02_SAR_RAW_0P/eoli/?count={count?}&name={geo:name?}&startPage={startPage?}&startIndex={startIndex?}&sort={sru:sortKeys?}&q={searchTerms?}&start={time:start?}&stop={time:end?}&bbox={geo:box?}&geometry={geo:geometry?}&uid={geo:uid?}&processingCenter={eop:processingCenter?}&acquisitionStation={eop:acquisitionStation?}&orbitDirection={eop:orbitDirection?}&orbitNumber={eop:orbitNumber?}&frame={eop:wrsLatitudeGrid?}&track={eop:wrsLongitudeGrid?}&swath={eop:swathIdentifier?}&polarisation={sar:polarisationChannels?}&submitted={dct:dateSubmitted?}", url3.Template.ToString());

            OpenSearch.Osdd.Url url4 = urls.ToArray()[4];
            Assert.AreEqual(0, url4.IndexOffset);
            Assert.AreEqual(0, url4.PageOffset);
            Assert.AreEqual("text/html", url4.MimeType);
            Assert.AreEqual("http://eo-virtual-archive4.esa.int/search/ER02_SAR_RAW_0P/html/?count={count?}&name={geo:name?}&startPage={startPage?}&startIndex={startIndex?}&sort={sru:sortKeys?}&q={searchTerms?}&start={time:start?}&stop={time:end?}&bbox={geo:box?}&geometry={geo:geometry?}&uid={geo:uid?}&processingCenter={eop:processingCenter?}&acquisitionStation={eop:acquisitionStation?}&orbitDirection={eop:orbitDirection?}&orbitNumber={eop:orbitNumber?}&frame={eop:wrsLatitudeGrid?}&track={eop:wrsLongitudeGrid?}&swath={eop:swathIdentifier?}&polarisation={sar:polarisationChannels?}&submitted={dct:dateSubmitted?}", url4.Template.ToString());

            OpenSearch.Osdd.Url url5 = urls.ToArray()[5];
            Assert.AreEqual(0, url5.IndexOffset);
            Assert.AreEqual(0, url5.PageOffset);
            Assert.AreEqual("application/vnd.google-earth.kml+xml", url5.MimeType);
            Assert.AreEqual("http://eo-virtual-archive4.esa.int/search/ER02_SAR_RAW_0P/kml/?count={count?}&name={geo:name?}&startPage={startPage?}&startIndex={startIndex?}&sort={sru:sortKeys?}&q={searchTerms?}&start={time:start?}&stop={time:end?}&bbox={geo:box?}&geometry={geo:geometry?}&uid={geo:uid?}&processingCenter={eop:processingCenter?}&acquisitionStation={eop:acquisitionStation?}&orbitDirection={eop:orbitDirection?}&orbitNumber={eop:orbitNumber?}&frame={eop:wrsLatitudeGrid?}&track={eop:wrsLongitudeGrid?}&swath={eop:swathIdentifier?}&polarisation={sar:polarisationChannels?}&submitted={dct:dateSubmitted?}", url5.Template.ToString());

            OpenSearch.Osdd.Url url6 = urls.ToArray()[6];
            Assert.AreEqual(0, url6.IndexOffset);
            Assert.AreEqual(0, url6.PageOffset);
            Assert.AreEqual("application/atom+xml", url6.MimeType);
            Assert.AreEqual("http://eo-virtual-archive4.esa.int/search/ER02_SAR_RAW_0P/atom/?count={count?}&name={geo:name?}&startPage={startPage?}&startIndex={startIndex?}&sort={sru:sortKeys?}&q={searchTerms?}&start={time:start?}&stop={time:end?}&bbox={geo:box?}&geometry={geo:geometry?}&uid={geo:uid?}&processingCenter={eop:processingCenter?}&acquisitionStation={eop:acquisitionStation?}&orbitDirection={eop:orbitDirection?}&orbitNumber={eop:orbitNumber?}&frame={eop:wrsLatitudeGrid?}&track={eop:wrsLongitudeGrid?}&swath={eop:swathIdentifier?}&polarisation={sar:polarisationChannels?}&submitted={dct:dateSubmitted?}", url6.Template.ToString());
            List<OpenSearch.TemplateParameter> params6 = url6.Template.TemplateParameters.ToList<OpenSearch.TemplateParameter>();
            Assert.AreEqual(20, params6.Count);
            OpenSearch.TemplateParameter param4 = params6[4];
            Assert.AreEqual(OpenSearch.TemplateParameterKind.OptionalSubstitution, param4.Kind);
            Assert.AreEqual("sort", param4.Name);
            Assert.AreEqual(OpenSearch.Constants.NSOpenSearchSearchRetrievalViaUrl, param4.SubstitutionParameterValue.Namespace);
            Assert.AreEqual("sortKeys", param4.SubstitutionParameterValue.Parameter);

            ICollection<OpenSearch.Osdd.Image> images = osdd.Images;
            Assert.AreEqual(2, images.Count);
            OpenSearch.Osdd.Image image0 = images.ToArray()[0];
            Assert.AreEqual(38, image0.Height);
            Assert.AreEqual(120, image0.Width);
            Assert.AreEqual("image/png", image0.MimeType);
            Assert.AreEqual("http://www.terradue.com/images/terradue.png", image0.Uri.ToString());
            OpenSearch.Osdd.Image image1 = images.ToArray()[1];
            Assert.AreEqual(16, image1.Height);
            Assert.AreEqual(16, image1.Width);
            Assert.AreEqual("image/vnd.microsoft.icon", image1.MimeType);
            Assert.AreEqual(new System.Uri("http://www.terradue.com/images/terradue.ico"), image1.Uri);

            List<string> languages = osdd.Languages.ToList();
            Assert.AreEqual(1, languages.Count);
            Assert.AreEqual("en-us", languages[0]);

            List<string> outputEncodings = osdd.OutputEncodings.ToList();
            Assert.AreEqual(1, outputEncodings.Count);
            Assert.AreEqual("UTF-8", outputEncodings[0]);

            List<string> inputEncodings = osdd.InputEncodings.ToList();
            Assert.AreEqual(1, inputEncodings.Count);
            Assert.AreEqual("UTF-8", inputEncodings[0]);
        }
    }
}